package com.boarsoft.boar.batch.biz;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.batch.biz.BatchEventBiz;
import com.boarsoft.boar.batch.entity.BatchEvent;
import com.boarsoft.common.Util;
import com.boarsoft.common.biz.SimpleBizImpl;
import com.boarsoft.common.dao.PagedResult;

@Component
public class BatchEventBizImpl extends SimpleBizImpl implements BatchEventBiz {

	@Override
	@Transactional
	public void save(BatchEvent o) {
		if (Util.strIsEmpty(o.getId())) {
			dao.save(o);
		} else {
			dao.update(o);
		}
	}

	@Override
	@Transactional(readOnly = true)
	public PagedResult<BatchEvent> list(String exeId, String endTime, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append("from ").append(BatchEvent.class.getName());
		sb.append(" where exeId = '").append(exeId).append("'");
		if (Util.strIsNotEmpty(endTime)) {
			sb.append(" and endTime <= '").append(endTime).append("'");
		}
		String hql = sb.toString();
		int total = dao.getTotal("select count(id) ".concat(hql));
		List<BatchEvent> lt = dao.list(hql, pageNo, pageSize);
		return new PagedResult<BatchEvent>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional(readOnly = true)
	public BatchEvent get(String id) {
		return dao.get(BatchEvent.class, id);
	}
}